<template>
  <div class="mine">
    <div class="log">
      <div class="box">
        <div class="tabs">
          <ul>
            <li><a>工作空间</a></li>
            <li><a>购物空间</a></li>
            <p><span></span></p>
            <i><span></span></i>
          </ul>
        </div>
        <div class="show">
          <a href="">
            <img />
          </a>
          <div>
            <p @click="jumpLogin">登录/注册</p>
          </div>
        </div>
      </div>
    </div>
    <div class="opg">
      <ul>
        <li>
          <i class="iconfont">&#xe600;</i>
          <span>待付款</span>
        </li>
        <li>
          <i class="iconfont">&#xe7a2;</i>
          <span>待收货</span>
        </li>
        <li>
          <i class="iconfont">&#xe615;</i>
          <span>待评价</span>
        </li>
        <li>
          <i class="iconfont">&#xe69d;</i>
          <span>售后/退货</span>
        </li>
      </ul>
    </div>
    <div class="box">
      <div class="oph">
        <ul>
          <li>
            <i class="iconfont">&#xe664;</i>
            <span>真快乐余额币</span>
          </li>
          <li>
            <i class="iconfont">&#xe654;</i>
            <span>真快乐券</span>
          </li>
          <li>
            <i class="iconfont">&#xe68b;</i>
            <span>真快乐豆</span>
          </li>
          <li>
            <i class="iconfont">&#xe718;</i>
            <span>美通卡</span>
          </li>
        </ul>
      </div>
      <van-divider>你可能喜欢</van-divider>
      <div class="homer">
        <van-list
          class="home"
          v-model="loading"
          :finished="isEnd"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="shopCard"
            v-for="item in products"
            @click="jumpToDetail(item.id)"
            :key="item.id"
          >
            <img :src="item.image" alt="" />
            <h5>{{ item.title }}</h5>
            <p class="p2">
              <i>￥</i>
              {{ item.price }}
              <span class="cart"></span>
            </p>
          </div>
        </van-list>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategories, getHots } from '@/api/shop'

export default {
  name: 'Mine',
  data() {
    return {
      // 分类数组
      categories: [],
      products: [], // 推荐商品
      isEnd: false, // 是否所有推荐商品已查完
      nextIndex: 0, // 下一页推荐商品查询起始索引
      loading: true,
    }
  },
  async created() {
    // 实例创建后，立即发送网络请求
    // console.log('created...')

    try {
      const [{ list: categories }, { list: products, isEnd, nextIndex }] =
        await Promise.all([getCategories(), getHots()])
      // 修改 data 数据，以响应式渲染
      this.categories = categories
      this.products = products
      this.nextIndex = nextIndex
      this.isEnd = isEnd
      this.loading = false
    } catch (error) {
      console.error('有错误：', error)
    }
  },

  methods: {
    /**
     * 触底加载更多商品
     */
    async onLoad() {
      // 异步更新数据
      const { list, nextIndex, isEnd } = await getHots(this.nextIndex)
      this.products = [
        ...this.products, // 浅拷贝，将原有 products 数组中的元素复制到新数组中
        ...list,
      ]
      this.nextIndex = nextIndex
      this.isEnd = isEnd
      this.loading = false
    },
    jumpLogin() {
      this.$router.push('/login')
    },
  },
}
</script>

<style lang="less" scoped>
@font-face {
  font-family: "iconfont"; /* project id 3293649 */
  src: url("//at.alicdn.com/t/font_3293450_6zt2aeetne9.woff2") format("woff2"),
  url("//at.alicdn.com/t/font_3293450_6zt2aeetne9.woff") format("woff"),
  url("//at.alicdn.com/t/font_3293450_6zt2aeetne9.ttf") format("truetype"),
  url("#iconfont") format("svg");
}
@font-face {
  font-family: "iconfont"; /* project id 3293649 */
  src: url("");
  src: url("?#iefix") format("embedded-opentype"),
  url("//at.alicdn.com/t/font_3293649_y7xpifinipf.woff2") format("woff2"),
  url("//at.alicdn.com/t/font_3293649_y7xpifinipf.woff") format("woff"),
  url("//at.alicdn.com/t/font_3293649_y7xpifinipf.ttf") format("truetype"),
  url("#iconfont") format("svg");
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 28px;
  font-style: normal;
  color: rgb(135, 135, 135);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.log {
  height: 320px;
  background: #d02ed2;
  background-size: 100%;
  overflow: hidden;
  .box {
    background: url("../../assets/2.png") 50% no-repeat;
    overflow: hidden;
  }
}
.log .tabs {
  width: 100%;
  height: 70px;
  // background-color: aqua;
  margin-top: 20px;
  ul {
    width: 80%;
    height: 70px;
    //  background-color: aqua;
    margin-left: 190px;
    display: flex;
    li {
      width: 30%;
      height: 70px;
      margin: 14px 4px;

      a {
        font-size: 17px;
        font-weight: 500;
        color: #fff;
        font-style: normal;
      }
    }
    p {
      height: 70px;
      width: 70px;
      position: absolute;
      display: inline-block;
      background-size: 100%;
      margin: -6px 390px;
      span {
        height: 70px;
        width: 70px;
        display: inline-block;
        background: url("../../assets/3.gif") 50% no-repeat;
        background-size: 100%;
      }
    }
    i {
      height: 70px;
      width: 70px;
      position: absolute;
      display: inline-block;
      background-size: 100%;
      margin: 20px 490px;
      span {
        height: 37px;
        width: 37px;
        display: inline-block;
        background: url("../../assets/3.png") 50% no-repeat;
        background-size: 100%;
      }
    }
  }
}

.show {
  width: 80%;
  height: 120px;
  // background-color: dimgrey;
  margin: 50px 55px;
  a {
    display: block;
    width: 117px;
    height: 117px;
    border-radius: 100px;
    background-color: white;
    float: left;
  }
  p {
    // display: block;
    float: left;
    font-weight: 700;
    margin-left: 40px;
    font-size: 16px;
    color: #fff;
  }
}
.opg {
  height: 200px;
  overflow: hidden;

  ul {
    display: flex;
    height: 160px;
    justify-content: space-around;
    flex-wrap: wrap;
    li {
      width: 20%;
      flex-wrap: wrap;
      flex-direction: column;
      text-align: center;
      display: flex;
      box-sizing: border-box;
      padding: 35px 0;
      span {
        display: inline-block;
        margin: 12px 0;
        font-size: 13px;
        color: rgb(94, 92, 92);
      }
    }
  }
}
.box {
  overflow: hidden;
  background-color: #f8f8f8;
}
.oph {
  height: 200px;
  overflow: hidden;
  margin: 27px 0;
  background-color: white;

  ul {
    display: flex;
    height: 160px;
    justify-content: space-around;
    flex-wrap: wrap;
    li {
      width: 20%;
      flex-wrap: wrap;
      flex-direction: column;
      text-align: center;
      display: flex;
      box-sizing: border-box;
      padding: 35px 0;
      span {
        display: inline-block;
        margin: 12px 0;
        font-size: 13px;
        color: rgb(94, 92, 92);
      }
    }
  }
}
.home {
  width: 100%;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  .shopCard {
    width: 43%;
    height: 6rem;
    display: flex;
    flex-direction: column;
    border-radius: 20px;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px 10px;

    img {
      height: 70%;
      width: 100%;
      border-radius: 15px;
      background-size: 100%;
    }
    h5 {
      height: 0.5rem;
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      border-top: 1px dashed gainsboro;
      text-overflow: ellipsis;
      margin: 5px 2px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .p2 {
      font-size: 18px;
      color: #b20fd3;
      font-weight: 600;
      margin-top: 6px;
      display: flex;
      line-height: 50px;

      i {
        font-style: normal;
        font-size: 11px;
        font-weight: 600;
      }
      .cart {
        width: 40px;
        height: 40px;
        background-color: #b20fd3;
        line-height: 40px;
        position: absolute;
        background: url("../../assets/1.png") no-repeat 50%/90% content-box
        content-box;
        margin-left: 270px;
      }
    }
  }
}
</style>
